package com.booth.base.model;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Model;
/***
 * 模块
 * @TypeName：Modules
 * @description：
 * @author：Administrator
 */
public class Modules extends Model<Modules>
{
  public static final Modules dao = new Modules();
  private static final long serialVersionUID = 1L;
  private List<Modules> children = new ArrayList();
  /***
   * 获取子级模块
   * @return
   */
  public List<Modules> getChildren() {
    return this.children;
  }

  public void setChildren(List<Modules> subModules) {
    this.children = subModules;
  }
  /***
   * 查找全部的模块
   * @return
   */
  public List<Modules> findAll()
  {
    return dao.find("select * from modules ORDER BY show_order asc");
  }
  /***
   * 根据可用组id获取改组具有的模块
   * @param groupId：可用组id
   * @return
   */
  public List<Modules> findGroupAvalibe(Integer groupId)
  {
    return dao.find("select * from modules where status=9  and id in(select modules_id from groups_modules where group_id=?) order by show_order ", new Object[] { groupId });
  }
  /***
   * 获取子级模块，用于拼凑树形菜单数据
   * @param pid：父级id
   * @param status：状态
   * @param field：需要的字段
   * @return
   */
  public List<Modules> findSubModule(Integer pid, Short status, String field)
  {
    String sql = "SELECT " + field + " FROM modules WHERE ";
    if (pid == null)
    {
      sql = sql + " parent_id is null";
    }
    else
    {
      sql = sql + " parent_id=" + String.valueOf(pid);
    }
    if (status != null)
    {
      sql = sql + " AND status>=" + String.valueOf(status);
    }
    sql = sql + " ORDER BY show_order asc";
    return dao.find(sql);
  }
}